#include "MapTool.h"
#include "MapCanvas.h"
#include "MapRenderer.h"
#include <TGS/MapControl2.0/MapToPixel.h>
#include <QAction>
#include <QAbstractButton>

namespace TGS{
namespace MapControl{

MapTool::MapTool( MapCanvas* canvas )
    : QObject( canvas )
	, _canvas( canvas )
	, _cursor( Qt::CrossCursor )
	, _action( nullptr )
	, _button( nullptr )
{
}


MapTool::~MapTool()
{
  _canvas->unsetMapTool( this );
}


MapTool::Point MapTool::toMapCoordinates( const QPoint& point )
{
  return _canvas->getCoordinateTransform()->screenToMap( point );
}

QPoint MapTool::toCanvasCoordinates( const Point& point )
{
  double x = point.getX(), y = point.getY();
  return _canvas->getCoordinateTransform()->mapToScreen( x, y );  
}


void MapTool::activate()
{
  // make action and/or button active
	if ( _action )
		_action->setChecked( true );
	if ( _button )
		_button->setChecked( true );

	// set cursor (map tools usually set it in constructor)
	_canvas->setCursor( _cursor );
}


void MapTool::deactivate()
{
  if ( _action )
    _action->setChecked( false );
  if ( _button )
    _button->setChecked( false );
}

void MapTool::setAction( QAction* action )
{
  _action = action;
}

QAction* MapTool::action()
{
  return _action;
}

void MapTool::setButton( QAbstractButton* button )
{
	_button = button;
}

QAbstractButton* MapTool::button()
{
  return _button;
}


void MapTool::canvasMoveEvent( QMouseEvent *e )
{
  Q_UNUSED( e );
}

void MapTool::canvasDoubleClickEvent( QMouseEvent *e )
{
  Q_UNUSED( e );
}

void MapTool::canvasPressEvent( QMouseEvent *e )
{
  Q_UNUSED( e );
}

void MapTool::canvasReleaseEvent( QMouseEvent *e )
{
  Q_UNUSED( e );
}

void MapTool::keyPressEvent( QKeyEvent *e )
{
  Q_UNUSED( e );
}

void MapTool::keyReleaseEvent( QKeyEvent *e )
{
  Q_UNUSED( e );
}
void MapTool::renderComplete()
{
}

bool MapTool::isTransient()
{
  return false;
}

bool MapTool::isEditTool()
{
  return false;
}

MapCanvas* MapTool::canvas()
{
  return _canvas;
}

void MapTool::canvasWheelEvent( QWheelEvent *e )
{
	Q_UNUSED(e);
}

void MapTool::setCursor( const QCursor& cursor )
{
	_cursor = cursor;
	 _canvas->setCursor( _cursor );
}

void MapTool::reset()
{

}


}//MapControl
}//TGS